//
//  XHF_LoginViewController.m
//  haojia
//
//  Created by 徐宏飞 on 16/5/4.
//  Copyright © 2016年 徐宏飞. All rights reserved.
//

#import "XHF_LoginViewController.h"
#import "XHF_RegSeccodeViewController.h"

#import "XHF_AFN.h"




@interface XHF_LoginViewController ()<NetworkToolerDelegate, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *tfdUserName;
@property (weak, nonatomic) IBOutlet UITextField *tfdPwd;

@property (weak, nonatomic) IBOutlet UIView *baseView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollV;

@property (nonatomic, strong) UILabel *lblUserName;
@property (nonatomic, strong) UILabel *lblPwd;

@property (nonatomic, strong) UITapGestureRecognizer *tap;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgIconButtom;

@property (nonatomic, strong) NetworkTooler *networkTool;

@end

@implementation XHF_LoginViewController




#pragma mark - life cycle

- (void)initNavigationBar
{
    self.enableReturnButton = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"密码登录";
    _networkTool = [NetworkTooler new];

    
    
    self.tfdUserName.leftView = self.lblUserName;
    self.tfdUserName.leftViewMode = UITextFieldViewModeAlways;
    
    
    self.tfdPwd.leftView = self.lblPwd;
    self.tfdPwd.leftViewMode = UITextFieldViewModeAlways;
    
    
    [_baseView addGestureRecognizer:self.tap];
    
    
    
    
    
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    

    self.scrollV.scrollEnabled = NO;
    
    self.lblUserName.frame = CGRectMake(0, 0, 40, 20);
    self.lblPwd.frame = CGRectMake(0, 0, 40, 20);
    
    
    if ( self.tfdUserName) {
        
        [self _username];
    }
    
    if (SCREEN_HEIGHT == 480) {
        self.imgIconButtom.constant = 30;
    }
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    self.view.backgroundColor = UIColorFromRGB(0xFAFAFA);
    //监听键盘的弹出事件
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated{
    //移除监听
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - data

-(void) _passwordLoginRequst{
    
    NSString *md5 = [CJMD5 md5HexDigest:self.tfdPwd.text];
    
    
    NSDictionary *parameters = @{
                                 
                                 @"mobile":self.tfdUserName.text,
                                 @"password":md5
                                 
                                 };

    _networkTool.delegate = self;
    [_networkTool parameters:parameters withURL:Login_show withIsPaparameters:YES];
    
}

#pragma mark - NetworkToolerDelegate

- (void)returnData:(id)data{
   
    if (isNSDictionary) {
        
    
    NSString *sessionid = [data objectForKey:@"session_id"];
    NSString *worker_id = [data objectForKey:@"worker_id"];
    [MobClick profileSignInWithPUID:self.tfdUserName.text];

        Locatianmanager *locationmanager = [Locatianmanager shareManager];
        [locationmanager startLocation];
    //将数据写入本地
    
    NSDictionary *loginData = [NSDictionary dictionaryWithObjectsAndKeys:
                               sessionid, @"sessionid",
                               worker_id,@"worker_id",
                               @"1", @"loginSucceed",
                               nil];
    
    
    [[NSUserDefaults standardUserDefaults] setObject:self.tfdUserName.text forKey:@"mobile"];
    
    [[NSUserDefaults standardUserDefaults] setObject:loginData forKey:@"loginData"];
    //同步数据
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    //刷新mine页面
    
    //遍历导航控制器子控制
    [self.navigationController popViewControllerAnimated:NO];
    [self.delegate popTorootVC];
    
    }


}
- (void)returnRequestError:(NSError *)error{
    [self showAlertWithTitle:@"温馨提示" message:@"登录失败" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];


}
- (void)returnMsg:(NSString *)Msg{
    //失败
//    NSString *msg = [json objectForKey:@"msg"];
    [self showAlertWithTitle:@"温馨提示" message:Msg cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
}


#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    [self loginBtnClick:nil];
    return YES;
}




#pragma mark - event response
- (IBAction)regAction:(id)sender {
    //进入重置密码
    XHF_RegSeccodeViewController *reg = [XHF_RegSeccodeViewController new];
    reg.title = @"重置密码";
    reg.smsType = SMSTYP_WORK_EFORGET;
    [self.navigationController pushViewController:reg animated:YES];
    
    
    
}




- (IBAction)loginBtnClick:(id)sender {
    
    
    //过滤登录条件
    if ([self.tfdUserName.text isEqualToString:@""]) {
        [self showAlertWithTitle:@"温馨提示" message:@"账户不能为空" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    
    if (![self PhoneNumIsvalid:self.tfdUserName.text]) {
        [self showAlertWithTitle:@"温馨提示"message:@"账号不合法" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    
    
    if ([self.tfdPwd.text isEqualToString:@""]) {
        [self showAlertWithTitle:@"温馨提示" message:@"密码不能为空" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    
    if (![self passWordCodeIsvalid:self.tfdPwd.text]) {
        [self showAlertWithTitle:@"密码不合法" message:@"密码只能由6-20位的字母和数字组成" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    
    
    
    [self _passwordLoginRequst];
    
    
}






#pragma mark - private methods

-(void) _username{
    ZhuangxiuManager *manager = [ZhuangxiuManager shareManager];
    if (manager.mobile != nil) {
        self.tfdUserName.text = manager.mobile;
    }
}


- (void)keyboardWillChangeFrame:(NSNotification *)notification{
    //获取键盘的Y值
    CGRect rect = [[notification.userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    CGFloat keybordY = rect.origin.y;
    CGFloat tranformValue = keybordY - self.view.frame.size.height;
    
    CGFloat value = 0.0;
    if (SCREEN_HEIGHT == 667) {  //6s
        value = 0;
    }
    if (SCREEN_HEIGHT == 568) {  //5 5s
        value = 0.6;
    }
    
    if (SCREEN_HEIGHT == 480) {  // 4s
        value = 1;
    }
    
    if (tranformValue >= 0) {
        tranformValue = 0;
    }
    
    CGPoint point = CGPointMake(0, -tranformValue*value);
    _scrollV.contentOffset = point;
    
}

- (void)TapAction{
    [self.view endEditing:YES];
}




#pragma mark - gettsers and setters
-(UILabel *)lblUserName{
    
    if (!_lblUserName) {
        _lblUserName = [[UILabel alloc] init];
        _lblUserName.text = @"手机:";
        _lblUserName.font = [UIFont systemFontOfSize:13];
        
    }
    
    return _lblUserName;
}

- (UILabel *)lblPwd{
    if (!_lblPwd) {
        _lblPwd = [[UILabel alloc] init];
        _lblPwd.text = @"密码:";
        _lblPwd.font = [UIFont systemFontOfSize:13];
    }
    return _lblPwd;
}

- (UITapGestureRecognizer *)tap{
    if (!_tap) {
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapAction)];
    }
    
    return _tap;
}



@end
